cmake_minimum_required(VERSION 3.17)
project(networkDisk)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH "D:/software/Qt/5.9.9/mingw53_32")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(QT_VERSION 5)
set(REQUIRED_LIBS
        Core
        Gui
        Widgets
        Sql
        Network)
find_package(Qt5Core REQUIRED)

set(Boost_INCLUDE_DIR D:/env/cinclude/boost_1_81_0)
set(Boost_LIBRARY_DIR D:/env/cinclude/boost_1_81_0/libs)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

qt5_add_resources(qrc_FILES Sources/res/res.qrc)
include_directories(Sources/headers)
set(REQUIRED_LIBS_QUALIFIED
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Sql
        Qt5::Network)

add_executable(${PROJECT_NAME}
        main.cpp
        ${qrc_FILES}
        Sources/src/Login.cpp
        Sources/headers/Login.h
        Sources/headers/ui_Login.h
        Sources/headers/clickedlabel.h
        Sources/src/clickedlabel.cpp
        Sources/src/MainWindow.cpp
        Sources/headers/MainWindow.h
        Sources/src/MainWindow.ui
        Sources/src/User.cpp
        Sources/headers/User.h
        Sources/headers/sqlModule.h
        Sources/src/sqlModule.cpp
        Sources/src/userWidget.cpp
        Sources/headers/userWidget.h
        Sources/src/talkTabWidget.cpp
        Sources/headers/talkTabWidget.h
        Sources/src/talkTabWidget.ui
        Sources/src/connectSocket.cpp
        Sources/headers/connectSocket.h
        Sources/src/fileTabWidget.cpp
        Sources/headers/fileTabWidget.h
        Sources/src/fileTabWidget.ui
        Sources/src/fileLabel.cpp
        Sources/headers/fileLabel.h Sources/src/talkTextEdit.cpp Sources/headers/talkTextEdit.h)

if (NOT CMAKE_PREFIX_PATH)
    message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
            "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED} ${Boost_LIBRARIES})
if (WIN32)
    set(DEBUG_SUFFIX)
    if (CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB ${REQUIRED_LIBS})
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
